package com.youtui.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import net.sf.jxls.transformer.XLSTransformer;

/**
 * excel工具类
 * 
 * @author wj
 * 
 */
@Component
public class ExcelUtil {

	/**
	 * 导出数据
	 * 
	 * @param request
	 * @param response
	 * @param fileName
	 *            表名
	 * @param templateName
	 *            表格模板名
	 * @param list
	 *            导出数据
	 * @throws Exception
	 */
	public void excelData(HttpServletRequest request, HttpServletResponse response, String fileName,
			String templateName, List<?> datalist, Map<String, Object> datamap) throws Exception {
		String contextPath = request.getSession().getServletContext().getRealPath("");
		contextPath = contextPath + File.separator + "template" + File.separator;
		String tempFilename = contextPath + templateName + System.nanoTime() + ".xlsx";
		String templateFilename = contextPath + templateName + ".xlsx";
		File templateFile = new File(templateFilename);
		File tempFile = new File(tempFilename);
		FileInputStream templateFileIn = new FileInputStream(templateFile);
		FileOutputStream tempFileOut = new FileOutputStream(tempFile);// 拷贝模板文件作为临时文件
		// byte[] b = new byte[1024];
		int length = 3 * 1024 * 1024;
		byte[] buffer = new byte[length];
		while (true) {
			int ins = templateFileIn.read(buffer);
			if (ins == -1) {
				templateFileIn.close();
				tempFileOut.flush();
				tempFileOut.close();
				break;
			} else {
				tempFileOut.write(buffer, 0, ins);
			}
		}

		XLSTransformer transformer = new XLSTransformer();
		InputStream in = null;
		OutputStream out = null;
		Map<String, Object> beans = new HashMap<>();
		response.setHeader("Content-Disposition",
				"attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1") + ".xlsx");
		response.setContentType("application/vnd.ms-excel");

		if (datamap != null) {
			beans.putAll(datamap);
		}
		beans.put("datalist", datalist);
		try {
			in = new BufferedInputStream(new FileInputStream(tempFilename));
			Workbook workbook = transformer.transformXLS(in, beans);
			// XSSFSheet sheet=(XSSFSheet) workbook.getSheetAt(0);
			out = response.getOutputStream();
			workbook.write(out);
			if (tempFile.exists()) {
				tempFile.delete();// 删除临时文件
			}
			out.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
				}
			}
		}

	}
}
